﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace RadioStationPOC.Views
{
    /// <summary>
    /// ConfiguringWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ConfiguringWindow : Window
    {
        public ConfiguringWindow()
        {
            InitializeComponent();
        }
        private void Label_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            this.WindowState = this.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
        }

        private void DockPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void WindowMin_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void WindowClose_Click(object sender, RoutedEventArgs e)
        {
            this.Visibility = System.Windows.Visibility.Hidden;
        }

        private void WindowMax_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = this.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
        }
        string ReciveIP = ConfigurationManager.AppSettings["ReciveIP"];
        string RecivePort = ConfigurationManager.AppSettings["RecivePort"];
        string SendIP = ConfigurationManager.AppSettings["SendIP"];
        string SendPort = ConfigurationManager.AppSettings["SendPort"];
        string HeartBeatService = ConfigurationManager.AppSettings["HeartBeatService"];
        string CompetitionDbConnStr = ConfigurationManager.AppSettings["CompetitionDbConnStr"];
        string OperLogDbConnStr = ConfigurationManager.AppSettings["OperLogDbConnStr"];
        string AccountManageServiceStr = AppConfigSave.GetEndpointAddress("AccountManageService");
        string DispatcherClientServiceStr = AppConfigSave.GetEndpointAddress("DispatcherClientService");
        //获取要自动运行的应用程序名
        string strName = AppDomain.CurrentDomain.BaseDirectory + "ET.ScoreTeacherClient.exe";
        bool isLoaded = false;
        /// <summary>
        /// 加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //本地
            string[] Local = ReciveIP.Split('.');
            if (Local.Length > 3)
            {
                Localtbx1.Text = Local[0];
                Localtbx2.Text = Local[1];
                Localtbx3.Text = Local[2];
                Localtbx4.Text = Local[3];
                LocalPorttbx.Text = RecivePort;
            }
            //数据库
            string[] DbConn = CompetitionDbConnStr.Split(new char[3] { '=', '.', ';' });
            if (DbConn.Length > 6)
            {
                DbConntbx1.Text = DbConn[1];
                DbConntbx2.Text = DbConn[2];
                DbConntbx3.Text = DbConn[3];
                DbConntbx4.Text = DbConn[4];
                DbConnNametbx.Text = DbConn[6];
                DbConnIdtbx.Text = DbConn[8];
            }
            //数据库
            string[] OperLog = OperLogDbConnStr.Split(new char[3] { '=', '.', ';' });
            if (DbConn.Length > 6)
            {
                OperLogtbx1.Text = OperLog[1];
                OperLogtbx2.Text = OperLog[2];
                OperLogtbx3.Text = OperLog[3];
                OperLogtbx4.Text = OperLog[4];
                OperLogNametbx.Text = OperLog[6];
                OperLogIdtbx.Text = OperLog[8];
            }
            string[] Service = SendIP.Split('.');
            if (Service.Length > 3)
            {
                Servicetbx1.Text = Service[0];
                Servicetbx2.Text = Service[1];
                Servicetbx3.Text = Service[2];
                Servicetbx4.Text = Service[3];
                ServicePorttbx.Text = SendPort;
            }
            string[] Account = AccountManageServiceStr.Split(new char[3] { '/', '.', ':' });
            if (Account.Length > 7)
            {
                Accounttbx1.Text = Account[3];
                Accounttbx2.Text = Account[4];
                Accounttbx3.Text = Account[5];
                Accounttbx4.Text = Account[6];
                AccountPorttbx.Text = Account[7];
            }
            string[] Dispatcher = DispatcherClientServiceStr.Split(new char[3] { '/', '.', ':' });
            if (Dispatcher.Length > 7)
            {
                Dispatchertbx1.Text = Dispatcher[3];
                Dispatchertbx2.Text = Dispatcher[4];
                Dispatchertbx3.Text = Dispatcher[5];
                Dispatchertbx4.Text = Dispatcher[6];
                DispatcherPorttbx.Text = Dispatcher[7];
            }
            isLoaded = true;
        }

        /// <summary>
        /// 保存配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SaveBut_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("确定保存设置吗？保存后将退出程序请重新启动。", "", MessageBoxButton.YesNoCancel, MessageBoxImage.Question) == MessageBoxResult.Yes)
            {
                //保存数据库
                string DbConnStr = "Server=" + DbConntbx1.Text.Trim() + "." + DbConntbx2.Text.Trim() + "."
                    + DbConntbx3.Text.Trim() + "." + DbConntbx4.Text.Trim() + ";Database=" + DbConnNametbx.Text.Trim() + "; User id=" + DbConnIdtbx.Text.Trim() + ";pwd=ats001;";
                AppConfigSave.UpDataToAppKey("CompetitionDbConnStr", DbConnStr);

                //保存数据库
                string OperLogStr = "Server=" + OperLogtbx1.Text.Trim() + "." + OperLogtbx2.Text.Trim() + "."
                    + OperLogtbx3.Text.Trim() + "." + OperLogtbx4.Text.Trim() + ";Database=" + OperLogNametbx.Text.Trim() + "; User id=" + OperLogIdtbx.Text.Trim() + ";pwd=ats001;";
                AppConfigSave.UpDataToAppKey("OperLogDbConnStr", OperLogStr);

                //保存本地
                string LocalStr = Localtbx1.Text.Trim() + "." + Localtbx2.Text.Trim() + "."
                   + Localtbx3.Text.Trim() + "." + Localtbx4.Text.Trim();
                AppConfigSave.UpDataToAppKey("ReciveIP", LocalStr);
                //保存本地端口
                string RecivePortStr = LocalPorttbx.Text.Trim();
                AppConfigSave.UpDataToAppKey("RecivePort", RecivePortStr);
                //保存服务器
                string ServiceStr = Servicetbx1.Text.Trim() + "." + Servicetbx2.Text.Trim() + "."
                   + Servicetbx3.Text.Trim() + "." + Servicetbx4.Text.Trim();
                AppConfigSave.UpDataToAppKey("SendIP", ServiceStr);
                //保存服务器端口
                string SendPortStr = ServicePorttbx.Text.Trim();
                AppConfigSave.UpDataToAppKey("SendPort", SendPortStr);
                //心跳服务终结点配置
                string DispatcherStr = "http://" + Dispatchertbx1.Text.Trim() + "." + Dispatchertbx2.Text.Trim() + "."
                   + Dispatchertbx3.Text.Trim() + "." + Dispatchertbx4.Text.Trim() + ":" + DispatcherPorttbx.Text.Trim() + "/DispatcherClientService";
                AppConfigSave.SetEndpointAddress("DispatcherClientService", DispatcherStr);

                //保存服务器
                string ScoreServiceStr = "http://" + Dispatchertbx1.Text.Trim() + "." + Dispatchertbx2.Text.Trim() + "."
                   + Dispatchertbx3.Text.Trim() + "." + Dispatchertbx4.Text.Trim() + ":" + DispatcherPorttbx.Text.Trim() + "/ScoreService";
                AppConfigSave.SetEndpointAddress("ScoreService", ScoreServiceStr);
                //保存服务器
                string EMapServiceStr = "http://" + Dispatchertbx1.Text.Trim() + "." + Dispatchertbx2.Text.Trim() + "."
                   + Dispatchertbx3.Text.Trim() + "." + Dispatchertbx4.Text.Trim() + ":" + DispatcherPorttbx.Text.Trim() + "/EMapService";
                AppConfigSave.SetEndpointAddress("EMapService", EMapServiceStr);
                //保存服务器
                string DriverClientServiceStr = "http://" + Dispatchertbx1.Text.Trim() + "." + Dispatchertbx2.Text.Trim() + "."
                   + Dispatchertbx3.Text.Trim() + "." + Dispatchertbx4.Text.Trim() + ":" + DispatcherPorttbx.Text.Trim() + "/DriverClientService";
                AppConfigSave.SetEndpointAddress("DriverClientService", DriverClientServiceStr);
                //保存服务器
                string DriverManageServiceStr = "http://" + Dispatchertbx1.Text.Trim() + "." + Dispatchertbx2.Text.Trim() + "."
                   + Dispatchertbx3.Text.Trim() + "." + Dispatchertbx4.Text.Trim() + ":" + DispatcherPorttbx.Text.Trim() + "/DriverManageService";
                AppConfigSave.SetEndpointAddress("DriverManageService", DriverManageServiceStr);
                //保存服务器
                string AccountManageServiceStr = "http://" + Dispatchertbx1.Text.Trim() + "." + Dispatchertbx2.Text.Trim() + "."
                   + Dispatchertbx3.Text.Trim() + "." + Dispatchertbx4.Text.Trim() + ":" + DispatcherPorttbx.Text.Trim() + "/AccountManageService";
                AppConfigSave.SetEndpointAddress("AccountManageService", AccountManageServiceStr);
                //保存服务器
                string DatabaseServiceStr = "http://" + Dispatchertbx1.Text.Trim() + "." + Dispatchertbx2.Text.Trim() + "."
                   + Dispatchertbx3.Text.Trim() + "." + Dispatchertbx4.Text.Trim() + ":" + DispatcherPorttbx.Text.Trim() + "/DatabaseService";
                AppConfigSave.SetEndpointAddress("DatabaseService", DatabaseServiceStr);
                //保存服务器
                string DispatcherManageServiceStr = "http://" + Dispatchertbx1.Text.Trim() + "." + Dispatchertbx2.Text.Trim() + "."
                   + Dispatchertbx3.Text.Trim() + "." + Dispatchertbx4.Text.Trim() + ":" + DispatcherPorttbx.Text.Trim() + "/DispatcherManageService";
                AppConfigSave.SetEndpointAddress("DispatcherManageService", DispatcherManageServiceStr);

                ////保存服务器
                //string AccountStr = "http://" + Accounttbx1.Text.Trim() + "." + Accounttbx2.Text.Trim() + "."
                //   + Accounttbx3.Text.Trim() + "." + Accounttbx4.Text.Trim() + ":" + AccountPorttbx.Text.Trim() + "/AccountManageService";
                //AppConfigSave.SetEndpointAddress("AccountManageService", AccountStr);


                if (MainWindow.CurMainWindow.AudioIdentify != null)
                    MainWindow.CurMainWindow.AudioIdentify.Exit();
                //System.Diagnostics.Process.Start(strName);
                App.Current.Shutdown();
            }
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelBut_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 值改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Localtbx_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (isLoaded)
            {
                TextBox textBox = sender as TextBox;
                var strMath = textBox.Text.Trim();
                int math;
                if (!int.TryParse(strMath, out math))
                {
                    MessageBox.Show("请输入正确的IP地址");
                    return;
                }
                if (math > 255 && math < 0)
                {
                    MessageBox.Show("请输入正确的IP地址");
                    return;
                }
            }
        }


        public void WinPosition()
        {
            #region 设置窗体大小及停靠位置
            //设置窗体宽度
            this.Width = SystemParameters.PrimaryScreenWidth * 0.5;
            //设置窗体高度   
            this.Height = SystemParameters.PrimaryScreenHeight * 1;
            this.Top = 0;
            this.Left = 0;
            #endregion
        }
    }
}
